package io.github.eiot.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.github.eiot.codec.BCDTime;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author yan
 * @since 2025-09-07
 */
public class BCDTimeSerializer extends JsonSerializer<BCDTime> {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void serialize(BCDTime bcdTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        LocalDateTime localDateTime = bcdTime.toLocalDateTime();
        jsonGenerator.writeString(localDateTime.format(formatter));
    }
}
